//
//  MessageViewController.m
//  EWork
//
//  Created by administrator on 14-11-28.
//  Copyright (c) 2014年 gaobo. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageDetailViewController.h"
#import "RecentMsgListCell.h"


@interface MessageViewController ()

@end

@implementation MessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    XMPPManager *manager = [XMPPManager sharedInstance];
    [manager goOnline];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMsgCome:) name:kXMPPNewMsgNotifaction object:nil];
}

- (void)newMsgCome:(NSNotification *)notifacation
{
#warning 怎么让对应的消息列表的未读消息数+1
    [self refresh];
}

- (void)refresh{
    self.msgArray=[MessageObject fetchRecentChatByPage:1];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.msgArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"messageCell";
    RecentMsgListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil){
        cell = [[RecentMsgListCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    MessageUserUnionObject *unionObject = [self.msgArray objectAtIndex:indexPath.row];
    [cell setUnionObject:unionObject];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"Message2MessageDetail" sender:nil];
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"Message2MessageDetail"]){
        NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
        MessageDetailViewController *md =[[MessageDetailViewController alloc]init];
        md = segue.destinationViewController ;
        md.unionObject =[self.msgArray objectAtIndex:indexPath.row];
    }
}
@end
